#include <vector>
#include <type_traits>
#include <iostream>
#include <algorithm>
using namespace std;

template <typename T, typename = enable_if_t<is_arithmetic<T>::value>>
int find_most(vector<T> nums)
{
    int max_occur = 0, max = 0;
    std::sort(nums.begin(), nums.end());
    for (int i = 0; i < nums.size(); i++)
    {
        int count = 1;
        while (i + 1 < nums.size() && nums[i + 1] == nums[i])
            count++, i++;

        // 如果有个数出现次数超过半数，直接返回
        if (count > nums.size() / 2)
            return nums[i];

        if (count > max_occur)
            max_occur = count, max = nums[i];
    }
    return max;
}

int main()
{
    vector<int> nums{1, 3, 2, 5, 2, 3, 2, 9, 3, 3, 5, 2, 4};
    cout << find_most(nums) << endl;	// 2
    return 0;
}